﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace FormLib
{
    public partial class SellPriceForm : DevExpress.XtraEditors.XtraForm
    {
        //
        private IFeatureLayer m_FeatureaLayer;
        private IFeature m_zdFeature;
        private int m_zdjg2;
        public SellPriceForm(IFeatureLayer zdtcFeatureaLayer, IFeature zdFeature,int zdjg2,string zdbh,string zdlx,string crjg)
        {
            if (zdjg2 == -1) { MessageBox.Show("该宗地价格不正确。"); return; }
            InitializeComponent();
            m_FeatureaLayer=zdtcFeatureaLayer;
            m_zdFeature=zdFeature;
            this.txtEZDBH.Text = zdbh;
            m_zdjg2 = zdjg2;
            this.txtEZDDJ2.Text = zdjg2.ToString ();
            this.txtEZDLX.Text = zdlx;
            this.txtCRJG.Text=(crjg.Equals("")) ? ("出让价格未计算...") : (crjg);
        }

        private void sBtnCal_Click(object sender, EventArgs e)
        {

            if (this.sBtnCal.Text == "测算完成")
            {   
                this.Close();
                return;
            }
            if (this.txtEZDDJ2.Text == "")
            {
                MessageBox.Show("宗地价格未计算。");
                return;
            }
            double crje = m_zdjg2 * 0.4;
            this.txtCRJG.Text = crje.ToString();
            //入库：
            try
            {
                IDataset pDataset = (IDataset)m_FeatureaLayer.FeatureClass;
                if (null == pDataset)
                {
                    MessageBox.Show("宗地图层为空！");
                    return;
                }

                #region 初始化要更新字段的字段名、字段Index(Array)
                //string fieldName = "ZDJG";
                #endregion

                IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
                try
                {

                    #region 开始编辑
                    if (!pWorkspaceEdit.IsBeingEdited())
                    {
                        pWorkspaceEdit.StartEditing(true);
                    }
                    pWorkspaceEdit.StartEditOperation();
                    #endregion
                    m_zdFeature.set_Value(m_zdFeature.Fields.FindField("CRJG"), crje);
                    m_zdFeature.Store();

                    #region 结束编辑
                    pWorkspaceEdit.StopEditOperation();
                    if (!pWorkspaceEdit.IsBeingEdited())
                    {
                        COM.LIB.LOG.LogOperator.WriteLog("保存出错。宗地编号：" + this.txtEZDBH.Text);
                        this.sBtnCal.Text = "重新测算";
                    }
                    pWorkspaceEdit.StopEditing(true);
                    this.sBtnCal.Text = "测算完成";
                    #endregion
                }
                catch (Exception ex)
                {
                    COM.LIB.LOG.LogOperator.WriteLog(ex.Message);
                }
                finally
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pWorkspaceEdit);
                }
            }
            catch (Exception ex)
            {
                COM.LIB.LOG.LogOperator.WriteLog(ex.Message);
            }
        }
    }
}